﻿namespace NEvoWeb.Modules.NB_Store
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class TaxCalcController
    {
        private TaxController _objTaxCtrl = new TaxController();
        private NB_Store_TaxRatesInfo _objTaxInfo;
        private NB_Store_TaxRatesInfo _TaxDefault;
        private string _TaxOption;

        public TaxCalcController(int PortalID)
        {
            this._TaxDefault = this.getDefaultTaxValue(PortalID, "DTX");
            if (this._TaxDefault == null)
            {
                this._TaxDefault = new NB_Store_TaxRatesInfo();
                this._TaxDefault.Disable = false;
                this._TaxDefault.ItemID = -1;
                this._TaxDefault.ObjectID = -1;
                this._TaxDefault.TaxDesc = "Tax Default";
                this._TaxDefault.TaxPercent = Conversions.ToString(0);
                this._TaxDefault.TaxType = "DTX";
            }
            this._objTaxInfo = this.getDefaultTaxValue(PortalID, "OTX");
            if (this._objTaxInfo == null)
            {
                this._TaxOption = "1";
            }
            else
            {
                this._TaxOption = Conversions.ToInteger(this._objTaxInfo.TaxPercent).ToString();
            }
        }

        private bool checkPayVAT(string MerchantCC, string VATNumber, int PortalID, string CountryCode)
        {
            string str2 = SharedFunctions.GetStoreSetting(PortalID, "taxexempt.list", SharedFunctions.GetCurrentCulture());
            string str = SharedFunctions.GetStoreSetting(PortalID, "taxapplies.list", SharedFunctions.GetCurrentCulture());
            if ((str2 != "") && (Strings.InStr(str2.ToLower(), CountryCode.ToLower(), CompareMethod.Binary) > 0))
            {
                return false;
            }
            if ((str != "") && (Strings.InStr(str.ToLower(), CountryCode.ToLower(), CompareMethod.Binary) <= 0))
            {
                return false;
            }
            return ((((VATNumber == "") | Versioned.IsNumeric(VATNumber)) | (Strings.Len(VATNumber) < 3)) || (VATNumber.Substring(0, 2).ToUpper() == MerchantCC));
        }

        public CartTaxInfo getCartTaxDetails(int PortalID, decimal ShipAmount, string MerchantCC, string VATNumber)
        {
            CartTaxInfo info3 = new CartTaxInfo();
            OrderController controller = new OrderController();
            bool flag = this.checkPayVAT(MerchantCC, VATNumber, PortalID, CurrentCart.GetCurrentCart(PortalID).CountryCode);
            ArrayList currentCartItems = CurrentCart.GetCurrentCartItems(PortalID);
            info3.CartID = CurrentCart.GetCurrentCart(PortalID).CartID;
            info3.TaxOption = this._TaxOption;
            info3.TaxAmount = "0";
            info3.TotalGROSS = "0";
            info3.TotalNET = "0";
            info3.ShipTax = "0";
            if (this._TaxOption != "1")
            {
                IEnumerator enumerator=null;
                try
                {
                    enumerator = currentCartItems.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        NB_Store_CartItemsInfo current = (NB_Store_CartItemsInfo) enumerator.Current;
                        ProductTaxInfo info4 = this.getProductTaxDetails(PortalID, current);
                        if (info4 != null)
                        {
                            if (flag)
                            {
                                info3.TaxAmount = decimal.Add(Conversions.ToDecimal(info3.TaxAmount), Conversions.ToDecimal(info4.TaxAmount)).ToString();
                                info3.TotalGROSS = decimal.Add(Conversions.ToDecimal(info3.TotalGROSS), Conversions.ToDecimal(info4.TotalGROSS)).ToString();
                                info3.TotalNET = decimal.Add(Conversions.ToDecimal(info3.TotalNET), Conversions.ToDecimal(info4.TotalNET)).ToString();
                            }
                            else if (this._TaxOption == "2")
                            {
                                info3.TaxAmount = decimal.Add(Conversions.ToDecimal(info3.TaxAmount), decimal.Multiply(Conversions.ToDecimal(info4.TaxAmount), decimal.MinusOne)).ToString();
                                info3.TotalGROSS = decimal.Add(Conversions.ToDecimal(info3.TotalGROSS), Conversions.ToDecimal(info4.TotalGROSS)).ToString();
                                info3.TotalNET = decimal.Add(Conversions.ToDecimal(info3.TotalNET), Conversions.ToDecimal(info4.TotalNET)).ToString();
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                if (flag)
                {
                    info3.ShipTax = this.getShipTaxAmount(PortalID, ShipAmount).ToString();
                    return info3;
                }
                if (this._TaxOption == "2")
                {
                    info3.ShipTax = decimal.Multiply(this.getShipTaxAmount(PortalID, ShipAmount), decimal.MinusOne).ToString();
                    return info3;
                }
                info3.ShipTax = "0";
            }
            return info3;
        }

        public NB_Store_TaxRatesInfo getCategoryTaxDetails(int ModelID)
        {
            ProductController controller = new ProductController();
            NB_Store_ModelInfo model = controller.GetModel(ModelID, SharedFunctions.GetCurrentCulture());
            if (model != null)
            {
                NB_Store_ProductsInfo product = controller.GetProduct(model.ProductID, SharedFunctions.GetCurrentCulture());
                if (product != null)
                {
                    NB_Store_TaxRatesInfo info2 = this._objTaxCtrl.GetTaxRateByObjID(product.PortalID, product.TaxCategoryID, "CAT");
                    if (info2 != null)
                    {
                        if (info2.Disable)
                        {
                            return this._TaxDefault;
                        }
                        return info2;
                    }
                }
            }
            return this._TaxDefault;
        }

        public NB_Store_TaxRatesInfo getDefaultTaxValue(int PortalID, string TaxType)
        {
            ArrayList list = this._objTaxCtrl.GetTaxRatesList(PortalID, TaxType, "", "");
            if (list == null)
            {
                return null;
            }
            if (list.Count == 0)
            {
                return null;
            }
            return (NB_Store_TaxRatesInfo) list[0];
        }

        public ProductTaxInfo getProductTaxDetails(int PortalID, NB_Store_CartItemsInfo objCartDet)
        {
            ProductTaxInfo info2 = new ProductTaxInfo();
            NB_Store_TaxRatesInfo info3 = this.getTaxRateUsed(PortalID, objCartDet.ModelID);
            decimal cartProductCost = decimal.Multiply(decimal.Add(objCartDet.UnitCost, objCartDet.Discount), new decimal(objCartDet.Quantity));
            info2.CartID = CurrentCart.GetCurrentCart(PortalID).CartID;
            info2.ModelID = objCartDet.ModelID;
            info2.TaxPercent = info3.TaxPercent;
            info2.TaxAmount = this.getTaxAmount(cartProductCost, Conversions.ToDecimal(info3.TaxPercent)).ToString();
            info2.TotalNET = this.getTaxNET(cartProductCost, Conversions.ToDecimal(info3.TaxPercent)).ToString();
            info2.TotalGROSS = this.getTaxGROSS(cartProductCost, Conversions.ToDecimal(info3.TaxPercent)).ToString();
            return info2;
        }

        public decimal getShipTaxAmount(int PortalID, decimal ShipAmount)
        {
            ProductTaxInfo info2 = new ProductTaxInfo();
            NB_Store_TaxRatesInfo info = this.getDefaultTaxValue(PortalID, "STX");
            if (info == null)
            {
                return decimal.Zero;
            }
            string str = this._TaxOption;
            switch (str)
            {
                case "1":
                    return decimal.Zero;

                case "2":
                    return decimal.Subtract(ShipAmount, decimal.Multiply(decimal.Divide(ShipAmount, decimal.Add(100M, Conversions.ToDecimal(info.TaxPercent))), 100M));
            }
            if (str != "3")
            {
                decimal num=0;
                return num;
            }
            return decimal.Subtract(decimal.Multiply(ShipAmount, decimal.Add(decimal.One, decimal.Divide(Conversions.ToDecimal(info.TaxPercent), 100M))), ShipAmount);
        }

        private decimal getTaxAmount(decimal CartProductCost, decimal TaxPercent)
        {
            decimal num=0;
            if (decimal.Compare(CartProductCost, decimal.Zero) <= 0)
            {
                return decimal.Zero;
            }
            switch (this._TaxOption)
            {
                case "1":
                    return decimal.Zero;

                case "2":
                    return decimal.Subtract(CartProductCost, decimal.Multiply(decimal.Divide(CartProductCost, decimal.Add(100M, TaxPercent)), 100M));

                case "3":
                    return decimal.Subtract(decimal.Multiply(CartProductCost, decimal.Add(decimal.One, decimal.Divide(TaxPercent, 100M))), CartProductCost);
            }
            return num;
        }

        private decimal getTaxGROSS(decimal CartProductCost, decimal TaxPercent)
        {
            decimal num=0;
            if (decimal.Compare(CartProductCost, decimal.Zero) <= 0)
            {
                return decimal.Zero;
            }
            switch (this._TaxOption)
            {
                case "1":
                    return decimal.Zero;

                case "2":
                    return CartProductCost;

                case "3":
                    return decimal.Multiply(CartProductCost, decimal.Add(decimal.One, decimal.Divide(TaxPercent, 100M)));
            }
            return num;
        }

        private decimal getTaxNET(decimal CartProductCost, decimal TaxPercent)
        {
            decimal num=0;
            if (decimal.Compare(CartProductCost, decimal.Zero) <= 0)
            {
                return decimal.Zero;
            }
            switch (this._TaxOption)
            {
                case "1":
                    return decimal.Zero;

                case "2":
                    return decimal.Multiply(decimal.Divide(CartProductCost, decimal.Add(100M, TaxPercent)), 100M);

                case "3":
                    return CartProductCost;
            }
            return num;
        }

        public string getTaxOption()
        {
            return this._TaxOption;
        }

        public NB_Store_TaxRatesInfo getTaxRateUsed(int PortalID, int ModelID)
        {
            NB_Store_TaxRatesInfo info2 = this._objTaxCtrl.GetTaxRateByObjID(PortalID, ModelID, "PRD");
            if (info2 == null)
            {
                return this.getCategoryTaxDetails(ModelID);
            }
            if (info2.Disable)
            {
                return this.getCategoryTaxDetails(ModelID);
            }
            return info2;
        }
    }
}

